package com.souku.finance.application.ui.more;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.souku.finance.application.R;
import com.souku.finance.application.adapter.AlreadyRequestAdapter;
import com.souku.finance.application.adapter.NotRequestAdapter;
import com.souku.finance.application.adapter.ViewPagerAdapter;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.RequestHasBean;
import com.souku.finance.application.bean.RequestNoBean;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.ViewPagerIndicator;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.SuccessError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Android on 2016/12/20.
 */

public class RequestListActivity extends BaseActivity implements View.OnClickListener {

    protected ViewPagerIndicator idIndicator;
    private PullToRefreshListView already_listview;
    private PullToRefreshListView not_listview;
    private AlreadyRequestAdapter alreadyRequestAdapter;
    private NotRequestAdapter notRequestAdapter;
    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected ViewPager idVp;
    private View view1;
    private View view2;
    private int pageLeft = 1;
    private List<RequestHasBean.ListBean> listhas = new ArrayList<>();
    private int pageRight = 1;
    private List<RequestNoBean.ListBean> listno = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_requestlist);
        initView();
        view1 = LayoutInflater.from(this).inflate(R.layout.page_alreadyrequest, null);
        view2 = LayoutInflater.from(this).inflate(R.layout.page_notrequest, null);
        final ArrayList<View> views = new ArrayList<>();
        views.add(view1);
        views.add(view2);
        ViewPagerAdapter adapter = new ViewPagerAdapter(views);
        idVp.setAdapter(adapter);
        topTitle.setText("邀请记录");

        already_listview = (PullToRefreshListView) view1.findViewById(R.id.alreadyrequest_pull_list);
        alreadyRequestAdapter = new AlreadyRequestAdapter(listhas);
        already_listview.setMode(PullToRefreshBase.Mode.BOTH);
        already_listview.setAdapter(alreadyRequestAdapter);
        already_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageLeft = 1;
                listhas.clear();
                hasInvest();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageLeft++;
                hasInvest();
            }
        });

        not_listview = (PullToRefreshListView) view2.findViewById(R.id.notrequest_pull_list);
        notRequestAdapter = new NotRequestAdapter(listno);
        not_listview.setMode(PullToRefreshBase.Mode.BOTH);
        not_listview.setAdapter(notRequestAdapter);
        not_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                listno.clear();
                pageRight = 1;
                noInvest();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageRight++;
                noInvest();
            }
        });
        idIndicator.setViewPager(idVp, 0);
    }

    @Override
    public void internet() {
        not_listview.setRefreshing(true);
        already_listview.setRefreshing(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        not_listview.setRefreshing(true);
        already_listview.setRefreshing(true);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(RequestListActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        idVp = (ViewPager) findViewById(R.id.id_vp);
        idIndicator = (ViewPagerIndicator) findViewById(R.id.id_indicator);
    }

    private void hasInvest() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("pg_size", "10");
        map.put("pg_no", pageLeft + "");
        map.put("hasInvest", "1");
        RequestManager.getInstance().post(UrlConstact.FP_INVITATION_INFO, map, new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                already_listview.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    RequestHasBean bean = JsonUtil.fastBean(response, RequestHasBean.class);
//                    listhas = new JsonUtil().fastArrayBean(response, RequestHasBean.ListBean.class);
                    if (bean.getList().size() == 0) {
//                    if (listhas.size() == 0) {
                        if (pageLeft != 1)
                            ToastUtil.show("没有更多内容");
                    } else {
                        listhas.addAll(bean.getList());
                        alreadyRequestAdapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                already_listview.onRefreshComplete();
                ToastUtil.show("请求失败");
            }
        }), ParamUtil.getHeaders());
    }


    private void noInvest() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("pg_size", "10");
        map.put("pg_no", pageRight + "");
        map.put("hasInvest", "0");
        RequestManager.getInstance().post(UrlConstact.FP_INVITATION_INFO, map, new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                not_listview.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    RequestNoBean bean = JsonUtil.fastBean(response, RequestNoBean.class);
//                    listno = new JsonUtil().fastArrayBean(response, RequestNoBean.ListBean.class);
                    if (bean.getList().size() == 0) {
//                    if (listno.size() == 0) {
                        if (pageRight != 1)
                            ToastUtil.show("没有更多内容");
                    } else {
                        listno.addAll(bean.getList());
                        notRequestAdapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                not_listview.onRefreshComplete();
                ToastUtil.show("请求失败");
            }
        }
        ), ParamUtil.getHeaders());
    }
}
